﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ProjectBase.Core;
using ProjectBase.Core.Web;
using ProjectBase.Mvc;

using $prjsn$.Common;
using $prjsn$.Entity.Services.HowTos;

namespace $safeprojectname$.Restrictions
{
    [CLSCompliant(false)]
    public class SimpleEntityRenderHandler : ControlRenderHandler<SimpleEntity>
    {
        protected override void Handle(IVisualControl control, PreRenderControlEventArgs args
            , IEntityModel<SimpleEntity, IFilter> model)
        {
            if (!model.IsAction(Str.Actions.Detail, Str.Actions.New, Str.Actions.Edit))
            {
                return;
            }

            // TODO limit access... e.g. For ADMINISTRATOR

            var box = control as DefinitionListItem;
            var doNotRender = box.Is()
                    && box.Controls.FindControls(c => c.ID.IsEqual("NaturalKey")).Any()
                    && SecurityManager.User.CurrentRole.IsEqual("Administrator");

            if (doNotRender)
            {
                args.DoNotRender = true;
            }

            var prop = control as IPropertyControl;
            var showReadOnly = prop.Is()
                    && prop.ID.IsEqual("Description")
                    && SecurityManager.User.CurrentRole.IsEqual("Administrator");
            if (showReadOnly)
            {
                args.UseReadOnly = true;
            }
        }
    }
}
